package com.kingint.web.portal.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.sys.entity.SerProcessDefinesDeploy;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.mbg.sys.mapper.SerProcessDefinesDeployMapper;
import com.kingint.web.portal.serve.service.ActivitiCancelService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.vo.ActivitiParam;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ActivitiCancelServiceImpl implements ActivitiCancelService {

    @Autowired
    private RepositoryService repositoryService;//提供一系列管理流程部署和流程定义的API
    @Autowired
    private RuntimeService runtimeService;//在流程运行时对流程实例进行管理与控制
    @Autowired
    private TaskService taskService;//对流程任务进行管理，例如任务提醒、任务完成和创建任务等
    @Autowired
    private HistoryService historyService;//对流程的历史数据进行操作，包括查询、删除这些历史数据
    @Resource
    private SerProcessDefinesDeployMapper processDefinesDeployMapper;
    @Resource
    private UmsAdminService adminService;

    @Override
    public int processDeploy(ActivitiParam activitiParam) {
        //先查询是否已经完成部署
        QueryWrapper<SerProcessDefinesDeploy> wrapper = new QueryWrapper<>();
        wrapper.eq("name",activitiParam.getProcessName())
                .eq("url",activitiParam.getProcessUrl());
        SerProcessDefinesDeploy selectOne = processDefinesDeployMapper.selectOne(wrapper);
        Integer isDeploy = selectOne.getIsDeploy();
        int i=0;
        if(isDeploy==0){//部署
            Deployment deploy = repositoryService.createDeployment()
                    .addClasspathResource(activitiParam.getProcessUrl())
                    //.addClasspathResource("processes/leave.bpmn.png")
                    .name(activitiParam.getProcessName())
                    .deploy();
            System.out.println(deploy.getName());
            System.out.println("流程定义部署名称:" + deploy.getName());
            System.out.println("流程定义id:" + deploy.getId());

            //同时修改对应的状态
            selectOne.setIsDeploy(1);
            i = processDefinesDeployMapper.updateById(selectOne);
        }
        return i;
    }

    @Override
    public String startProcess(String processInstanceKey,String businessKey,HashMap<String, Object> variables) {
        ProcessInstance instance=null;
        if(StringUtils.isEmpty(variables)){
            instance = runtimeService.
                    startProcessInstanceByKey(processInstanceKey,businessKey);
        }else{
            instance = runtimeService.
                    startProcessInstanceByKey(processInstanceKey,businessKey,variables);
        }
        System.out.println("流程实例名称：" + instance.getName());
        System.out.println("流程关联的数据：" + instance.getBusinessKey());
        System.out.println("流程实例ID:" + instance.getId());
        System.out.println("流程定义ID:" + instance.getProcessDefinitionId());
        return instance.getId();
    }

    @Override
    public boolean complete(Integer id, String processInstanceKey, String businessKey, Map<String, Object> variables) {

//        UmsMember currentMember = adminService.getCurrentMember();//当前用户
//        String businessKey = processInstanceKey + "_" + currentMember.getHotelId() + "_" + id;
//        //查询运行时的信息
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(processInstanceKey)
                .processInstanceBusinessKey(businessKey)
                .singleResult();
        //完成流程
        taskService.complete(task.getId(),variables);
        return true;
    }


    @Override
    public boolean complete(Integer id, String processInstanceKey, String businessKey) {
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(processInstanceKey)
                .processInstanceBusinessKey(businessKey)
                .singleResult();
        //完成流程
        taskService.complete(task.getId());
        return true;
    }
}
